/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;

import fever.app.menu.FvContextMenu;
import fever.app.menu.FvContextMenuItem;
import fever.utils.Stringifier;

/**
 * Context menu managment.
 * 
 * <p>Use it to register / unregister and get menu from anywhere in 
 * your application.
 * 
 * <p>Example
 * {@code
 *   public static var MENU : String = "myMenu";
 *   public static var ITEM : String = "myItem";
 *   	
 *   public function run() : Void
 *   {
 *   	var item : FvContextMenuItem;
 *   	item = new FvContextMenuItem ("Disable sprite menu");
 *   	item.addEventListener ( ContextMenuEvent.MENU_ITEM_SELECT, this, _onItemClick );
 *   	
 *   	var m : FvContextMenu = new FvContextMenu ();
 *   	m.push (item );
 *   	Fever.application.contextMenu = m;
 *   	
 *   	
 *   	item = new FvContextMenuItem ("Pixlib");
 *   	item.url = "http://www.osflash.org/pixlib";
 *   	item.addEventListener ( ContextMenuEvent.MENU_ITEM_SELECT, this, _onItemClick );
 *   	
 *   	var squareMenu : FvContextMenu = new FvContextMenu( MENU );
 *   	squareMenu.push ( item, ITEM );
 *   	
 *   	var square : MovieClip = FeverStage.getInstance().createEmptyMovieClip( "square" );
 *   	square.beginFill( 0x000000, 100 );
 *   	square.lineTo( 100, 0 );
 *   	square.lineTo( 100, 100 );
 *   	square.lineTo( 0, 100 );
 *   	square.lineTo( 0, 0 );
 *   	
 *   	square.menu = squareMenu.menu;
 *   }
 *   
 *   private function _onItemClick ( event : ContextMenuEvent ) : Void
 *   {
 *   	FeverDebug.DEBUG ("select item " + event.getTarget().caption + " on " + event.mouseTarget );
 *   	
 *   	var item : FvContextMenuItem = FvContextMenuHelper.getItem ( MENU, ITEM );
 *   	item.enabled = false;
 *   }  
 * }
 * 
 * @see fever.app.menu.FvContextMenu
 * @see fever.app.menu.FvContextMenuItem
 * 
 * @author Romain Ecarnot
 */
class fever.app.menu.FvContextMenuHelper
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _m : Map = new Map();
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Registers a new {@code FContextMenu}.
	 * 
	 * @param menu {@code FContextMenu} instance
	 * @param id (optional) menu's id
	 * 
	 * @return {@code FContextMenuName} menu id 
	 */
	public static function register( menu : FvContextMenu, id : String ) : String
	{
		if( ! _m.containsKey( id ) )
		{
			_m.put( id, menu );
			return id;
		}
		else
		{
			return id;
		}
	}
	
	/**
	 * Unregisters passed-in {@code menu'is} from application.
	 * 
	 * @param id {@code FContextMenuName} menu's id
	 */
	public static function unregister( id : String ) : Void
	{
		_m.remove( id );
	}
	
	/**
	 * Returns {@code FContextMenu} registrated with passed-in {@code id}
	 * 
	 * @param id {@code FContextMenuName} menu's id
	 */
	public static function get( id : String ) : FvContextMenu
	{
		return _m.get( id );
	}
	
	/**
	 * Returns {@code FContextMenuItem} registrated with passed-in {@code itemID} 
	 * from {@code FContextMenu} menu registred with passed-in {@code menuID} id.
	 * 
	 * @param menuID menu's id
	 * @param itemID item's id
	 */
	public static function getItem( menuID : String, itemID : String ) : FvContextMenuItem
	{
		var menu : FvContextMenu = _m.get( menuID );
		if( menu )
		{
			return menu.getItem( itemID );
		}
		return null;
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructs a new {@code ContextMenuManager} instance.
	 */
	private function FvContextMenuHelper()
	{
		_m = new Map();
	}	
}